package com.huike.clues.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huike.clues.mapper.SysDataMapper;
import com.huike.clues.service.ISysDataService;
import com.huike.common.core.domain.entity.SysDictData;
import com.huike.common.core.domain.entity.SysDictDataDTO;
import com.huike.common.core.domain.entity.SysDictTypeDTO;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.DictUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * @Desciption SysDataServiceImpl
 * @Author LUXIN
 * @Date 2023-10-16
 */
@Service
public class SysDataServiceImpl extends ServiceImpl<SysDataMapper, SysDictData> implements ISysDataService {

    @Resource
    private SysDataMapper sysDataMapper;

    /**
     * 查询字典数据信息
     *
     * @param dictType
     * @return
     */
    @Override
    public List<SysDictDataDTO> list(String dictType) {
        return sysDataMapper.findListByType(dictType);
    }

    /**
     * 查询字典列表
     *
     * @param sysDictDataDTO
     * @return
     */
    @Override
    public TableDataInfo<List<SysDictDataDTO>> findList(SysDictDataDTO sysDictDataDTO) {
        // 根据字典类型找出对应的字典列表
        List<SysDictDataDTO> list = sysDataMapper.findListByDTO(sysDictDataDTO);
        // 设置cache Key
        String cacheKey = DictUtils.getCacheKey(sysDictDataDTO.getDictLabel());
        // 设置缓存
        DictUtils.setDictCache(cacheKey,list);
        return new TableDataInfo<>(list, list.size());
    }

    /**
     * 查询字典数据详细信息
     *
     * @param dictCode
     * @return
     */
    @Override
    public SysDictDataDTO findDetail(Integer dictCode) {
        return sysDataMapper.findByDictCode(dictCode);
    }

    /**
     * 新增字典数据
     *
     * @param dict
     */
    @Override
    public void addDictionary(SysDictDataDTO dict) {
        // 暂时补全
        dict.setCreateBy("admin");
        dict.setCreateTime(DateUtils.getNowDate());
        dict.setStatus("0");
        sysDataMapper.add(dict);
    }

    /**
     * 修改数字字典数据
     *
     * @param dict
     */
    @Override
    public void updateDictionary(SysDictDataDTO dict) {
        // 暂时补全
        dict.setUpdateBy("admin");
        dict.setUpdateTime(DateUtils.getNowDate());
        sysDataMapper.update(dict);
    }

    /**
     * 删除数字字典数据
     *
     * @param dictCodes
     */
    @Override
    public void deleteDictionary(String dictCodes) {
        // 直接删除
        sysDataMapper.delete(dictCodes);
    }

}
